﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GolfScoringApplication
{
    public partial class frmAddress : Form
    {
        private frmGolfer _frmParent;

        public frmAddress()
        {
            InitializeComponent();
        }

        private void LoadAddressTypes()
        {
            cboAddressType.Items.Clear();
            cboAddressType.Items.Add(AddressType.Home.ToString());
            cboAddressType.Items.Add(AddressType.Work.ToString());
            cboAddressType.Items.Add(AddressType.Other.ToString());
            cboAddressType.SelectedIndex = 0;
        }

        private void frmAddress_Load(object sender, EventArgs e)
        {
            LoadAddressTypes();
        }

        public void LoadFormVariables(frmGolfer parentform)
        {
            _frmParent = parentform;
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (IsOKToContinue())
            {
                //Save the data to a structure
                AddressType objType;

                switch (cboAddressType.SelectedItem.ToString())
                {
                    case "Home":
                        {
                            objType = AddressType.Home;
                            break;
                        }
                    case "Work":
                        {
                            objType = AddressType.Work;
                            break;
                        }
                    default:
                        {
                            objType = AddressType.Other;
                            break;
                        }
                }
                Address objNewAddress = new Address(txtName.Text, objType,
                    txtAddressOne.Text, txtAddressTwo.Text, txtAddressThree.Text,
                    txtCity.Text, txtState.Text, txtZip.Text);
                _frmParent.AddressAdd(objNewAddress);
                this.Close();
            }
        }

        private bool IsOKToContinue()
        {
            bool blReturnValue = true;
            if (txtName.Text.Length == 0)
            {
                blReturnValue = false;
                MessageBox.Show("You must enter a Name!");
                txtName.Focus();
                goto exit;
            }
            if (txtAddressOne.Text.Length == 0)
            {
                blReturnValue = false;
                MessageBox.Show("You must enter an Address!");
                txtAddressOne.Focus();
                goto exit;
            }
            if (txtCity.Text.Length == 0)
            {
                blReturnValue = false;
                MessageBox.Show("You must enter a City!");
                txtCity.Focus();
                goto exit;
            }
            if (txtState.Text.Length == 0)
            {
                blReturnValue = false;
                MessageBox.Show("You must enter a State!");
                txtState.Focus();
                goto exit;
            }
            if (txtZip.Text.Length == 0)
            {
                blReturnValue = false;
                MessageBox.Show("You must enter a Zip Code!");
                txtZip.Focus();
                goto exit;
            }

            exit:
            return blReturnValue;
        }


        private void frmAddress_ParentChanged(object sender, System.EventArgs e)
        {
            
        }
    }
}
